#include "nemu.h"
#include <stdlib.h>
#include <time.h>

CPU_state cpu;

const char *regsl[] = {"eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi"};
const char *regsw[] = {"ax", "cx", "dx", "bx", "sp", "bp", "si", "di"};
const char *regsb[] = {"al", "cl", "dl", "bl", "ah", "ch", "dh", "bh"};

void reg_test() {
	srand(time(0));
	uint32_t sample[8];
	uint32_t eip_sample = rand();
	cpu.eip = eip_sample;

	int i;
	for(i = R_EAX; i <= R_EDI; i ++) {
		sample[i] = rand();
		reg_l(i) = sample[i];
		assert(reg_w(i) == (sample[i] & 0xffff));
	}

	assert(reg_b(R_AL) == (sample[R_EAX] & 0xff));
	assert(reg_b(R_AH) == ((sample[R_EAX] >> 8) & 0xff));
	assert(reg_b(R_BL) == (sample[R_EBX] & 0xff));
	assert(reg_b(R_BH) == ((sample[R_EBX] >> 8) & 0xff));
	assert(reg_b(R_CL) == (sample[R_ECX] & 0xff));
	assert(reg_b(R_CH) == ((sample[R_ECX] >> 8) & 0xff));
	assert(reg_b(R_DL) == (sample[R_EDX] & 0xff));
	assert(reg_b(R_DH) == ((sample[R_EDX] >> 8) & 0xff));

	assert(sample[R_EAX] == cpu.eax);
	assert(sample[R_ECX] == cpu.ecx);
	assert(sample[R_EDX] == cpu.edx);
	assert(sample[R_EBX] == cpu.ebx);
	assert(sample[R_ESP] == cpu.esp);
	assert(sample[R_EBP] == cpu.ebp);
	assert(sample[R_ESI] == cpu.esi);
	assert(sample[R_EDI] == cpu.edi);

	assert(eip_sample == cpu.eip);
}

uint32_t get_reg_value(char* s){
	//printf("get_reg_value::s = %s\n", s);
    if(strcmp(s,"$eip")==0){
		printf("get_reg_value:: eip = %x\n", cpu.eip);
		return cpu.eip;
	}else if(strcmp(s,"$eax")==0){
		printf("get_reg_value:: eax = %x\n", cpu.eax);
		return cpu.eax;
	}else if(strcmp(s,"$ebx")==0){
		printf("get_reg_value:: ebx = %x\n", cpu.ebx);
		return cpu.ebx;
	}else if(strcmp(s,"$esp")==0){
		printf("get_reg_value:: esp = %x\n", cpu.esp);
		return cpu.esp;	
	}else if(strcmp(s,"$ebp")==0){
		printf("get_reg_value:: ebp = %x\n", cpu.ebp);
		return cpu.ebp;
	}else if(strcmp(s,"$edx")==0){
		printf("get_reg_value:: edx = %x\n", cpu.edx);
		return cpu.edx;
	}else if(strcmp(s,"$ecx")==0){
		printf("get_reg_value:: ecx = %x\n", cpu.ecx);
		return cpu.ecx;	
	}else if(strcmp(s,"$esi")==0){
		printf("get_reg_value:: esi = %x\n", cpu.esi);
		return cpu.esi;
	}else if(strcmp(s,"$edi")==0){
		printf("get_reg_value:: edi = %x\n", cpu.edi);
		return cpu.edi;		
	}else if(strcmp(s,"$zf")==0){
		printf("get_sf_value:: SF = %x\n", cpu.ZF);
		return cpu.ZF;				
	}else{
		return 0;
	}
}